package vn.vos;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.widget.Toast;

public class AndroidSystemChecking {
	/**general service manager*/
	private Context context;
	/**info*/
	private PackageManager pkgMgr;	
	/**network*/
	private ConnectivityManager connMgr;
	
	public AndroidSystemChecking() {
		pkgMgr = null;
		connMgr = null;
		context = null;
	}	
	
	public AndroidSystemChecking(PackageManager pm) {
		pkgMgr = pm;
		connMgr = null;
		context = null;
	}
	
	public AndroidSystemChecking(PackageManager pm, ConnectivityManager cm) {
		pkgMgr = pm;
		connMgr = cm;
		context = null;
	}
	
	public AndroidSystemChecking(PackageManager pm, ConnectivityManager cm, Context ct) {
		pkgMgr = pm;
		connMgr = cm;
		context = ct;
	}
	
	public AndroidSystemChecking(Activity activity) {
		pkgMgr = activity.getPackageManager();
		context = activity.getApplicationContext();
		connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
	}
	
	/**
	 * Checking that a package has been installed or not been installed yet
	 * @param pkName name of package which want to check
	 * @return true if the package was installed
	 * 		   false if the package was not installed	 
	 */
	public boolean checkInstalledPackage(String pkName) {		
		 try {
			ApplicationInfo ai = pkgMgr.getApplicationInfo(pkName, 0);
			if(ai!=null)
				return true;
		} catch (NameNotFoundException ex) {
			if(context!=null)
				Toast.makeText(context, "Cannot file " + pkName + "in device system", Toast.LENGTH_LONG).show();
			else
				ex.printStackTrace();
		}		
		
		return false;
	}
		
	public boolean isNetworkAvailable() {
        if(connMgr == null)
		    connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		else {
			try {
		        NetworkInfo[] info = connMgr.getAllNetworkInfo();
		        if (info != null) {
		           for (int i = 0; i < info.length; i++) {
		              if (info[i].getState() == NetworkInfo.State.CONNECTED) {
		                 return true;
		              }
		           }
		        }
			} catch(RuntimeException ex) {
				if(context!=null)
					Toast.makeText(context, "system error:" + ex.getMessage() , Toast.LENGTH_LONG).show();
				else
					ex.printStackTrace();
			}
	     }
        
       return false;
   }
	
   public boolean checkGPSState() {
	    LocationManager manager = (LocationManager) context.getSystemService( Context.LOCATION_SERVICE );
	    try {
		    if ( manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) 	    			    		
		    	return true;
	    } catch(SecurityException  ex) {
	    	if(context!=null)
				Toast.makeText(context, "system error:" + ex.getMessage() , Toast.LENGTH_LONG).show();
			else
				ex.printStackTrace();
	    } catch(IllegalArgumentException ex) {
	    	if(context!=null)
				Toast.makeText(context, "system error:" + ex.getMessage() , Toast.LENGTH_LONG).show();
			else
				ex.printStackTrace();
	    }
	    
	    return false;
   }
}
